package com.fesine.ssh.dao.impl;

import com.fesine.ssh.dao.SaleItemDao;
import com.fesine.ssh.model.SaleItem;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Fesine on 2016/2/23.
 */
@Repository("saleItemDao")
public class SaleItemDaoImpl implements SaleItemDao {
    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }

    @Override
    public SaleItem load(String id) {
        return (SaleItem)this.getCurrentSession().load(SaleItem.class,id);
    }

    @Override
    public SaleItem get(String id) {
        return (SaleItem) this.getCurrentSession().get(SaleItem.class, id);
    }

    @Override
    public String save(SaleItem entity) {
        return (String)this.getCurrentSession().save(entity);
    }

    @Override
    public void update(SaleItem entity) {
        this.getCurrentSession().update(entity);
    }

    @Override
    public void saveOrUpdate(SaleItem entity) {
        this.getCurrentSession().saveOrUpdate(entity);
    }

    @Override
    public void delete(String id) {
        this.getCurrentSession().delete(id);
    }

    @Override
    public void flush() {
        this.getCurrentSession().flush();
    }

    @Override
    public void persist(SaleItem entity) {
        this.getCurrentSession().persist(entity);
    }

    @Override
    public List<SaleItem> findAll() {
        String hql = String.format("from SaleItem order by salesOrderId");
        List<SaleItem> saleItemList =
                this.getCurrentSession().createQuery(hql).list();
        return saleItemList;
    }

    @Override
    public List<SaleItem> findAll(int offset, int pageSize) {
        String hql = String.format("from SaleItem order by salesOrderId");
        Query query = this.getCurrentSession().createQuery(hql).setCacheable(true);
        query.setFirstResult(offset);
        query.setMaxResults(pageSize);
        List<SaleItem> saleItemList= query.list();
        return saleItemList;
    }

    @Override
    public List<SaleItem> findByEntity(SaleItem entity) {
        return null;
    }

    @Override
    public List<SaleItem> findByEntity(SaleItem entity, int offset, int pageSize) {
        return null;
    }


}
